<?php
require_once (APPLICATION_DIR."/application/model/Submenu.php");
class Submenu extends DataObjects_Submenu
{	
	function getSubMenuTitle($parent_id)
	{
		$this->selectAdd();
		$this->selectAdd("menu_id,menu_title,menu_node_id,menu_order,menu_hide");
		$this->whereAdd();
		$this->whereAdd("menu_parent_id = $parent_id");
		$this->orderBy();
		$this->orderBy("menu_order");
		if ($this->find())
		{
			return $this;
		}
		else 
		{
			return false;
		}
	}
	
	function getShowSubmenu($parent_id)
	{
	    $this->selectAdd();
		$this->selectAdd("menu_id,menu_title,menu_node_id,menu_order");
		$this->whereAdd();
		$this->whereAdd("menu_parent_id = $parent_id");
		$this->whereAdd("menu_hide = 0");
		$this->orderBy();
		$this->orderBy("menu_order");
		if ($this->find())
		{
			return $this;
		}
		else 
		{
			return false;
		}
	}
	
	function getAllShowSubmenu()
	{
	    $this->selectAdd();
		$this->selectAdd("menu_id,menu_title,menu_parent_id,menu_node_id");
		$this->whereAdd();
		$this->whereAdd("menu_hide = 0");
		$this->orderBy();
		$this->orderBy("menu_parent_id, menu_order");
		if ($this->find())
		{
			return $this;
		}
		else 
		{
			return false;
		}
	}
	
	function getAllSubMenu()
	{
		$this->selectAdd();
		$this->selectAdd("menu_id,menu_title, menu_parent_id, menu_node_id");
		$this->whereAdd();
		$this->orderBy();
		$this->orderBy("menu_parent_id, menu_order");
		$this->find();		
		return $this;
	}
	
	function getSubmenuInfoByID($menu_id)
	{
		$this->selectAdd();
		$this->selectAdd("menu_id,menu_title,menu_node_id, menu_hide");
		$this->whereAdd();
		$this->whereAdd("menu_id = $menu_id");
		$this->find();
		return $this;
	}
	
	function prepareDropMenu($parent_id, $parent_url)
	{
	    $smarty		= DRX::load("smarty");
	    $dropmenu	= $this->getShowSubmenu($parent_id);

	    if ($dropmenu)
	    {
	        $smarty->assign("menu_url",$parent_url);
	        $smarty->assign("dropmenu",$dropmenu);
	        $drop_content = $smarty->fetch("public/dropmenu.tpl");
	        $smarty->assign("dropmenu$parent_id",$drop_content);
	    }
	}
	
	function prepareLeftPane($topmenu_id)
	{
		$smarty		= DRX::load("smarty");
		
		$top_url[ABOUT_ID]    = ABOUT_URL;
		$top_url[PROGRAMME_ID]= PROGRAMME_URL;
		$top_url[PARTNER_ID]  = PARTNER_URL;
		$top_url[TOOL_ID]     = TOOL_URL;
		
		$leftpane	= $this->getShowSubmenu($topmenu_id);
		$smarty->assign("leftpane",$leftpane);
		
		$smarty->assign("top_url",$top_url[$topmenu_id]);
		
		$leftmenu 	= $smarty->fetch("public/leftpane.tpl");
		$smarty->assign("leftmenu",$leftmenu);
		
		$has_left_menu	= true;
		$smarty->assign("has_left_menu",$has_left_menu);
	}
	
	function NodeID($menu_id)
	{
		$this->selectAdd();
		$this->selectAdd("menu_node_id");
		$this->whereAdd();
		$this->whereAdd("menu_id = $menu_id");
		$this->find();
		if ($this->fetch())
		{
			$result = $this->menu_node_id;
		}
		else 
		{
			$result = 0;
		}
		return $result;
	}
	
	function deleteSubmenu($menu_id)
	{
		$submenu	= new Submenu();
		$node_id	= $submenu->NodeID($menu_id);
		$node		= new Node();
		$node->node_id = $node_id;
		$node->delete();
		$this->menu_id = $menu_id;
		return $this->delete();
	}
	
	function getMenuContent($menu_id)
	{
		$sub_menu	= new Submenu();
		$node_id	= $sub_menu->NodeID($menu_id);
		
		$node		= new Node();
		$menuContent= $node->getNodeInfo($node_id);
		return $menuContent;
	}
	
	function prepareMenuContent($menu_id)
	{
	    $sub_menu	= new Submenu();
		$node_id	= $sub_menu->NodeID($menu_id);
		
		if ($node_id)
		{
            $node		= new Node();
            $node->preparePage($node_id);
            return true;
		}
		else 
		{
		    return false;
		}
	}
	
	function updateSubmenu($arrForm)
	{
		extract($arrForm);
		$this->menu_id = $menu_id;
		$this->menu_title = $menu_title;
		$this->menu_order = $menu_order;
		$this->menu_hide = $menu_hide;
		$this->update();
	}
	
	function addSubmenu($arrForm)
	{
		extract($arrForm);
		$this->menu_id = "";
		$this->menu_parent_id = $menu_parent_id;
		$this->menu_title = $menu_title;
		$this->menu_order = $menu_order;
		$this->menu_hide = $menu_hide;
		
		$node = new Node();
		$this->menu_node_id = $node->getNextNodeID();
		$this->insert();
	}
	
	function insertSubmenu($menu_title,$menu_order,$menu_hide)
	{
		$this->menu_title = $menu_title;
		$this->menu_order = $menu_order;
		$this->menu_hide = $menu_hide;
		$this->insert();		
	}
	
	function getLastSubmenuID()
	{
	    $this->selectAdd();
		$this->selectAdd("menu_id");
		$this->orderBy("menu_id DESC");
		$this->find();
		if ($this->fetch())
		{
			$result = $this->menu_id;
		}
		else 
		{
			$result = 0;
		}
		return $result;
	}
}
?>